package com.zzzyh.blogbyspring.interceptor;

import com.zzzyh.blogbyspring.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("user_token");
        if(!StringUtils.hasLength(token)){
            response.setStatus(401);
            return false;
        }
        Claims claims = JwtUtils.postToken(token);
        if(claims == null){
            response.setStatus(401);
            return false;
        }
        return true;
    }
}
